package com.chixing.controller;

import com.chixing.entity.User;
import com.chixing.service.IUserService;
import com.chixing.util.JWTUtils;
import com.chixing.util.Result;
import org.springframework.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private JWTUtils jwtUtils;

    @Autowired
    private IUserService userService;
    // 注册接口
    @PostMapping("/register")
    public ResponseEntity<Map<String, Object>> register(@RequestBody User user) {
        Map<String, Object> result = new HashMap<>();
        boolean success = userService.register(user);
        if (success) {
            result.put("code", 200);
            result.put("message", "注册成功");
        } else {
            result.put("code", 500);
            result.put("message", "手机号已注册");
        }
        return ResponseEntity.ok(result);
    }

    // 登录接口
    @PostMapping("/login")
    public Result login(@RequestBody Map<String, String> loginInfo) {
        String phone = loginInfo.get("phone");
        String password = loginInfo.get("password");
        String token = userService.login(phone, password);
        User user = userService.getUserByPhone(phone);
        Map<String, Object> result = new HashMap<>();
        if (token != null) {
            result.put("code", 200);
            result.put("message", "登录成功");
            result.put("token", token);
            result.put("userId",user.getUserId());
        } else {
            result.put("code", 500);
            result.put("message", "手机号或密码错误");
        }
        return Result.getSuccess(result);
    }
    @PostMapping("/reset-password")
    public ResponseEntity<Map<String, Object>> resetPassword(@RequestBody Map<String, String> resetInfo) {
        String phone = resetInfo.get("phone");
        String newPassword = resetInfo.get("newPassword");

        userService.resetUserPassword(phone, newPassword);

        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("message", "密码重置成功");
        return ResponseEntity.ok(result);
    }
    @PostMapping("/refresh")
    public ResponseEntity<Result> refreshToken(@RequestBody Map<String, String> request) {
        String refreshToken = request.get("refreshToken");
        try {
            // 验证refresh token有效性
            Integer userId = jwtUtils.getUserIdFromToken(refreshToken);
            // 生成新的access token
            String newAccessToken = jwtUtils.generateToken(userId);
            // 可选：生成新的refresh token实现刷新令牌轮换
            String newRefreshToken = jwtUtils.generateRefreshToken(userId);

            Map<String, Object> tokens = new HashMap<>();
            tokens.put("accessToken", newAccessToken);
            tokens.put("refreshToken", newRefreshToken);

            return ResponseEntity.ok(Result.getSuccess(tokens));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
                    .body(Result.loginFail("Invalid refresh token"));
        }
    }
}
